package com.test.springboot3.springboot3testdemo1.controller;

import com.test.springboot3.springboot3testdemo1.entity.OrderInfo;
import com.test.springboot3.springboot3testdemo1.entity.User;
import jakarta.validation.constraints.Size;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/7/28-22:13
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@CrossOrigin(originPatterns = "https://javastack.cn", maxAge = 3600)
@Validated
@Controller
public class ResponseBodyController {
  @CrossOrigin
  @GetMapping(value = "/user/json/{userId}", produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity getJsonUserInfo(
      @PathVariable("userId") @Size(min = 5, max = 8) String userId) {
    User user = new User(userId, "Tom", 18);
    return new ResponseEntity(user, HttpStatus.OK);
  }

  @GetMapping(value = "/user/xml/{userId}", produces = MediaType.APPLICATION_XML_VALUE)
  public ResponseEntity getXmlUserInfo(@PathVariable("userId") String userId) {
    User user = new User(userId, "Tom", 18);
    List<OrderInfo> orderList = new ArrayList<>();
    OrderInfo orderInfo1 = new OrderInfo("1234501", 999, new Date());
    OrderInfo orderInfo2 = new OrderInfo("1234502", 666, new Date());
    OrderInfo orderInfo3 = new OrderInfo("1234503", 777, new Date());
    orderList.add(orderInfo1);
    orderList.add(orderInfo2);
    orderList.add(orderInfo3);
    user.setOrderList(orderList);

    return new ResponseEntity(user, HttpStatus.OK);
  }

  @PostMapping(value = "/user/save")
  public ResponseEntity saveUser(@RequestBody @Validated User user) {
    user.setId("123456");
    return new ResponseEntity(user, HttpStatus.OK);
  }
}
